var is_complete = false;

$(function(){
	$('ul.menu li.initial a').click(function(event){
 			event.preventDefault();
 			$('div.artists').slideUp();
			$('h2.sIFR-replaced').fadeIn();
 			if($(this).hasClass('open')){
 				$(this).removeClass('open');
 			}else{
			$.getJSON('ajax_callbacks/cache_library.php?initial='+$(this).text()+'&action=menu',initial_menu);
 				$(this).addClass('loading');	
 						
 			}
 		});

	var settings = $('.settings');
	if(settings.hasClass('new_user')){
		$("div.content").load('about.php #ajax_content','',function(responseText, textStatus, XMLHttpRequest){
				sIFR.replaceElement(named({sSelector:"h2", sFlashSrc:"swf/tuffy.swf"}));
		});
	} else {
		settings.hide();
		$('li.initial').show();
	}
	settings.height('auto');
	$('.open_settings').click(function(){
		settings.slideToggle();
		$("ul.menu li.initial a").removeClass('open');
		$('div.artists').slideUp();
	});
	setup_ajax_links();

});

function setup_ajax_links(){
	$('a.by_ajax').click(function(event){
		$("div.content").load(this.href+' #ajax_content','',function(responseText, textStatus, XMLHttpRequest){
				sIFR.replaceElement(named({sSelector:"h2", sFlashSrc:"swf/tuffy.swf"}));
			if(is_complete){
				$('.progress').html('');
			}
		});

		event.preventDefault();

	});
}
	function get_next_page(page,batch_size){
		$.getJSON('ajax_callbacks/cache_library.php?page='+page+'&batch_size='+batch_size+'&action=cache',
			function(data,textStatus){
			  if(textStatus == 'success'){
			  	var total_pages = data['pages'];


			  	if(page < total_pages){
			  		get_next_page(page+batch_size,batch_size);
				  	var progress = Math.round(page/total_pages*100);
				  	$('.progress').text('Building library, '+progress+'%.');
			  	} else {
				  	$('.progress').html('<strong>Building library, complete!</strong>');
				  	is_complete = true;
			  	}
			  	for(var i=0;i<data['initials_found'].length;i++){
			  		$("ul.menu li.initial a:contains('"+data['initials_found'][i]+"')").parents('li').addClass("active")

			  	}
			}
		});
	}
	
	function initial_menu(data,textStatus){
		if(textStatus == 'success'){
			$("ul.menu li.initial a").removeClass('open');
			$("ul.menu li.initial a:contains('"+data['initial']+"')").addClass('open');
		
			
			$('div.artists').html("");
			var cols = Math.ceil(data['artists'].length/6);
			for(var i = 0;i<data['artists'].length;i++){
				if(i%cols == 0){
					$('div.artists').append('<ul/>');
				}
				$('div.artists ul:last').append('<li><a href="?artist='+data['artists'][i]['name']+'&id='+data['artists'][i]['id']+'">'+data['artists'][i]['name']+'</a></li>');
			}
			$('div.artists li a').click(function(e){
				e.preventDefault();
				$('div.artists a.loading').removeClass('loading');
				var href = this.href;
				if(href.indexOf('?') > 0){
					href = href.substring(href.indexOf('?'));
				}

				$(this).addClass('loading');
				$("div.content").load('ajax_callbacks/artist.php'+href,'',content_callback);
			});
			$('.artists h2.sIFR-replaced').hide();
			$('.settings').slideUp();
			$('div.artists').slideDown();
			$('ul.menu li.initial a.loading').removeClass('loading');
		}
	}
	
	function content_callback(responseText, textStatus, XMLHttpRequest) {
	  	this; // dom element
	  	if(is_complete){
			$('.progress').html('');
		}
		$('div.artists').slideUp();

		$('div.artists a.loading').removeClass('loading');
	  	/*$('div.item').hover(
			function(){
				$(this).find('h3').show();
			},
			function(){
				$(this).find('h3').hide();
			}
			
		);*/
		sIFR.replaceElement(named({sSelector:"h2", sFlashSrc:"swf/tuffy.swf"}));
	}
	
	function get_home(){
		$.getJSON('ajax_callbacks/cache_library.php?action=cloud',build_tag_cloud);
	}
	function get_next_tags(){
		$.getJSON('ajax_callbacks/cache_library.php?action=cache_tags',
			function(data,textStatus){
			  if(textStatus == 'success'){
			  	$.getJSON('ajax_callbacks/cache_library.php?action=cloud',function(inner_data,inner_textStatus){
			  	
			  		build_tag_cloud(inner_data,inner_textStatus);
			  		if(data['continue']){
				  		get_next_tags();
				  	}
			  	});
			  	
			  	
			}
		});
		
	}

	
	function build_tag_cloud(data,textStatus){
		if(textStatus == 'success'){
			

			var ul = $('ul.cloud');
			if(ul.length == 0){
				ul = $('<ul class="cloud"></ul>');
				$('div.content').append(ul);
			}
			var max = 1;
			$.each(data.tags,function(index,value){
				if(Number(value['playcount']) > max){
					max = Number(value['playcount']);
				}
			});
			
			var step = max/10;
			
			$.each(data.tags,function(index,value){
				var exists = false;
				$("ul.cloud li:contains('"+value['tag']+"')").each(function(i,li){
					if($(li).text() == value['tag']){
						exists = true;
						return;
					}
				});
				if(!exists){
					var li = $('<li/>');
					li.text(value['tag']);
					//li.attr('class',Math.floor(value['playcount']/step));
					//li.css('font-size', 10+(Math.floor(value['playcount']/step)*3));
					ul.append(li);
					

					
					var size = 10+(Math.floor(value['playcount']/step)*3);
					li.animate({fontSize: size+'px'}, 1500, function() {});
				}
			});


			
			
		}
	}
	
	
	